package meterMonitor.controller;

import meterMonitor.biz.CommunicationExceptionBiz;
import meterMonitor.biz.MeterBiz;
import meterMonitor.constand.DayOrNightEnum;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.util.excel.ExcelUtil;
import meterMonitor.util.excel.domain.ExcelColumn;
import meterMonitor.util.excel.domain.ExcelHead;
import meterMonitor.vo.QueryRequest;
import meterMonitor.vo.ToEverything;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/2/14 0014.
 */
@RestController
@RequestMapping("MonitorException")
public class MonitorExceptionLogController extends BaseController {
    @Autowired
    private CommunicationExceptionBiz communicationExceptionBiz;

    @Autowired
    private MeterBiz meterBiz;

    @RequestMapping("exceptionQuery.do")
    @FireAuthority(authorityTypes = AuthorityType.Edit)
    public ModelAndView index(ModelAndView mv) {
        return mv;
    }

    @RequestMapping("selectAll.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public List<ToEverything> selectAll(QueryRequest queryRequest) {
        String title = null;
        String content = null;
        List<ToEverything> list = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        Long endUTC=null;
        if (queryRequest.getEndDate()!=null){
            String endDate = simpleDateFormat.format(queryRequest.getEndDate());
            endUTC = queryRequest.getEndDate().getTime();
        }


      if (endUTC!=null){
          //这里有点小问题，end时间必须退后一天.
          endUTC = endUTC+24*60*60*1000;
          long end = new Long(endUTC);
          Date date = new Date(end);
          queryRequest.setEndDate(date);

      }


       try {
            //title 和 content 特别处理下中文乱码
            if (queryRequest.getTitle()!=null){
                //title = new String(queryRequest.getTitle().trim().getBytes("ISO-8859-1"),"UTF-8");
            }
            if (queryRequest.getContent()!=null){
                //content = new String(queryRequest.getContent().trim().getBytes("ISO-8859-1"),"UTF-8");
            }
           title=queryRequest.getTitle();
           content=queryRequest.getContent();
            queryRequest.setTitle(title);
            queryRequest.setContent(content);

            list = communicationExceptionBiz.selectAll(queryRequest);

            for (int i = 0; i<= list.size()-1;i++){
                list.get(i).setExceptionTime(list.get(i).getExceptionTime().substring(0,19));
                if (list.get(i).getDayOrNight().equals("10") ) {
                    list.get(i).setDayOrNight("白班");
                }
                if (list.get(i).getDayOrNight().equals("20") ) {
                    list.get(i).setDayOrNight("夜班");
                }
            }
            return list ;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
        }
        return list;
    }

    //导出功能
    @RequestMapping("exportExcel.do")
    @ResponseBody
    @FireAuthority(authorityTypes = AuthorityType.SystemAuth)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-yy HH-mm-ss");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String dayOrNight = request.getParameter("dayOrNight");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        Date sDate = null;
        Date eDate = null;
        try{
            if (startDate!=null&&startDate!=""){
                sDate = simpleDateFormat.parse(startDate);
            }
            if (endDate!=null&&endDate!=""){
                eDate = simpleDateFormat.parse(endDate);
            }

        }catch (Exception e){
            this.logger.error(e.getMessage(),e);
        }
        QueryRequest queryRequest = new QueryRequest();
        queryRequest.setContent(content);
        queryRequest.setDayOrNight(dayOrNight);
        queryRequest.setStartDate(sDate);
        queryRequest.setEndDate(eDate);
        queryRequest.setTitle(title);
        List<ToEverything> list = communicationExceptionBiz.selectAll(queryRequest);
        for (int i = 0; i<= list.size()-1;i++){
            list.get(i).setExceptionTime(list.get(i).getExceptionTime().substring(0,19));
            if (list.get(i).getDayOrNight().equals("10") ) {
                list.get(i).setDayOrNight("白班");
            }
            if (list.get(i).getDayOrNight().equals("20") ) {
                list.get(i).setDayOrNight("夜班");
            }
        }
        //导出
        ExcelHead head = new ExcelHead();
        head.setColumns(setColumns());
        //生成Excel文件流
        ExcelUtil.getInstanse().exportExcel(head, "导出模板", list, response);
    }

    //构建Excel列表
    private List<ExcelColumn> setColumns(){

        List<ExcelColumn> columns = new ArrayList<>();
        String[] fields = {"exceptionTime","anotherName", "title", "content", "exData", "remark"};
        String[] headers = {"异常时间","水表位置", "异常标题", "异常内容", "异常数据", "备注"};
        for (int i = 0; i < fields.length; i++) {
            columns.add(new ExcelColumn(i, fields[i], headers[i]));
        }
        return columns;
    }
}
